﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;

namespace Runda.EShop.Web.Customer
{
    public partial class CustomerAddress : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetAddress();
            }
        }

        private void GetAddress()
        {
            List<BizEntAddrEntity> list = new CustomerBll().GetAll<BizEntAddrEntity>(" and EntID = '" + base.User.Identity.LoginUser.EntID + "'");
            gvList.DataSource = list;
            gvList.DataBind();
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button btnSet = e.Row.FindControl("btnSet") as Button;
                BizEntAddrEntity entity = e.Row.DataItem as BizEntAddrEntity;
                btnSet.Visible = entity.IsDefault == "Y" ? false : true;
            }
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "set")
            {
                CustomerBll bll = new CustomerBll();
                Guid ID = new Guid(e.CommandArgument.ToString());
                BizEntAddrEntity entity = new BizEntAddrEntity(ID);
                entity.EntID = base.User.Identity.LoginUser.EntID;
                entity.IsDefault = "Y";
                bll.SetCustomerDefaultAddress(entity);

                GetAddress();
            }
        }

        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid ID = new Guid(gvList.DataKeys[e.RowIndex]["ID"].ToString());

            CustomerBll bll = new CustomerBll();
            bll.Delete<BizEntAddrEntity>(" and ID = '"+ID+"'");

            GetAddress();
        }
    }
}